#include "htsc_grpc_consumer_control_version.h"
#include "htsc_grpc_common_init.h"
#include <string>
#include <string.h>
#include <map>
#include <vector>
 
//用户存服务的版本约束，如果为空时，说明不需要做版本控制
static std::map<std::string, std::string> g_consumer_service_version;

static bool grpc_service_version_isinit;

 
//字符串拆分
std::vector<std::string> htsc_grpc_common_split(const std::string &s, const std::string &seperator) {
	std::vector<std::string> result;
	typedef std::string::size_type string_size;
	string_size i = 0;

	while (i != s.size()) {
		//找到字符串中首个不等于分隔符的字母；
		int flag = 0;
		while (i != s.size() && flag == 0) {
			flag = 1;
			for (string_size x = 0; x < seperator.size(); ++x)
				if (s[i] == seperator[x]) {
					++i;
					flag = 0;
					break;
				}
		}

		//找到又一个分隔符，将两个分隔符之间的字符串取出；
		flag = 0;
		string_size j = i;
		while (j != s.size() && flag == 0) {
			for (string_size x = 0; x < seperator.size(); ++x)
				if (s[j] == seperator[x]) {
					flag = 1;
					break;
				}
			if (flag == 0)
				++j;
		}
		if (i != j) {
			result.push_back(s.substr(i, j - i));
			i = j;
		}
	}
	return result;
}

//解析配置字符串，初始化map对象
//如果当前应用只调用一个服务，属性值直接配置版本号，例如1.0.0
//如果当前应用需要调用多个服务，属性值按照冒号逗号的方式分隔，
//例如com.htsc.examples.Greeter:1.0.0,com.htsc.examples.Hello:1.2.1
void htsc_grpc_service_version_init() {
	//保证值执行一次初始化
	if (grpc_service_version_isinit) {
		return;
	}

	//解析字符串放入map对象
	char * service_conf = htsc_grpc_consumer_service_version_get();
	if (service_conf == NULL || strlen(service_conf)==0) {
		return;
	}
	char * indexchar = strchr(service_conf, HTSC_GRPC_VERSION_SEPARATOR_CHAR);
	//不包含":",表示所有服务都是同一版本。
	if ((indexchar - service_conf) <= 0) { 
		g_consumer_service_version.insert(std::pair<std::string, std::string>(HTSC_GRPC_SERVICE_VERSION_COMMON_KEY, service_conf));
		return;
	}

	std::vector<std::string> services = htsc_grpc_common_split(std::string(service_conf), ",");
	std::vector<std::string> service_version;
	if (services.size() == 0) {
		return;
	}    
	for (int i = 0;i < services.size();i++) {
		if (services[i].empty()) {
			continue;
		}
		service_version = htsc_grpc_common_split(std::string(services[i]), HTSC_GRPC_VERSION_SEPARATOR_STR);
		if (service_version.size() != 2) {
			 continue;
		}
		g_consumer_service_version.insert(std::pair<std::string, std::string>(service_version[0], service_version[1]));
	}
}

//获取版本控制信息
std::map<std::string, std::string> htsc_grpc_consumer_control_version_get() {
	return g_consumer_service_version;
}

//获取版本控制信息
void htsc_grpc_consumer_control_version_update(char *service_name,char *service_version) {
	htsc_grpc_service_version_init();
	std::map<std::string, std::string >::iterator version_iter = g_consumer_service_version.find(service_name);
	if (version_iter != g_consumer_service_version.end()) {
		version_iter->second = service_version;
	}
	else {
		g_consumer_service_version.insert(std::pair<std::string, std::string>(service_name, service_version));
	}
}


 
//校验服务版本是否匹配
bool htsc_grpc_consumer_control_version_match(char *servername,char* version) {
	if (servername == NULL) {
		return false;
	}

	if (g_consumer_service_version.size() == 0) {
		return true;
	}

	std::map<std::string, std::string >::iterator version_iter = g_consumer_service_version.find(servername);

	//不做版本控制
	if (version_iter == g_consumer_service_version.end() || version_iter->second.empty()) {
		//查询是否值配置版本号，未指定服务
		version_iter = g_consumer_service_version.find(HTSC_GRPC_SERVICE_VERSION_COMMON_KEY);
		if (version_iter == g_consumer_service_version.end() || version_iter->second.empty()) {
			return true;
		}
	}
    //consumer指定版本，provider未指定版本
	if (version == NULL || strlen(version) == 0) {
		return false;
	}
	//匹配版本一致
	if (strcmp(version_iter->second.data(), version) == 0) {
		return true;
	}
	else {
		return false;
	}
}